package com.qf.test.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.test.mapper.DtsUserMapper;
import com.qf.test.pojo.DtsUser;
import com.qf.test.vo.DayStatis;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserServiceImpl implements  UserService {
    @Autowired
    private DtsUserMapper dtsUserMapper;
    @Override
    public int countUser() {
        QueryWrapper<DtsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted",false);
        Integer count = dtsUserMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public List<DayStatis> incrCount() {
        List<DayStatis> list = dtsUserMapper.staticIncreaseUser();
        return list;
    }

    /**
     * 会员首页的展示
     * @param page  第几页
     * @param limit  每页多少条数据
     * @param username  名字
     * @param mobile   手机号
     * @param sort  升序
     * @param order  降序
     * @return
     */
    @Override
    public IPage<DtsUser> findbyid(Integer page, Integer limit, String username, String mobile, String sort, String order) {

        //1.创建一个分页对象
        Page<DtsUser> ipag = new Page<>(page,limit);
        //2.创建查询对象
        QueryWrapper<DtsUser> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted",false);
        //如果前端传入的name为空 代表没有进行查询  不进行sql查询
        if(!StringUtils.isEmpty(username)){
            wrapper.like("username",username);
        }
        //如果前端传入的mobile为空 代表没有进行查询  不进行sql查询
        if(!StringUtils.isEmpty(mobile)){
            wrapper.like("mobile",mobile);
        }
        //判断排序是升序还是降序
        if(!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            if(order.equalsIgnoreCase("asc")){
                //升序
                wrapper.orderByAsc(sort);
            }else {
                //降序
                wrapper.orderByDesc(sort);
            }
        }
        //调用dao层查询返回结果
        Page<DtsUser> list = dtsUserMapper.selectPage(ipag, wrapper);
        return list;
    }
}
